<?php

/**
 * 
 * @author yuxun
 *
 */
class SdkServerController extends AdminBaseController {

	public function index(){
		$this->setTitle('SDK服务器管理');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$filter->addFilter('server_name', 'input', 'SDK服务器名称');
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = SdkServerService::indexHeader();
		if(PubFunc::isAjax()){
			$result = SdkServerService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=sdkServer/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'sdkServer/index',
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
	
		}
	}
	

    public function add(){
        $this->setTitle('新建SDK服务器');

    }

    public function doAdd(){
    	$count = SdkServerService::crepeat(0, 'server_name', $this->R('server_name'),$this->R('g'));
    	if($count > 0){
    		$this->showMessage('名称已经存在');
    	}else{
    		$result = SdkServerService::doAdd($_POST);
    		
    		if($result !== false){
    			$this->showMessage('新增成功', PubFunc::buildUrl("/oss/?r=sdkServer/index"));
    		}else{
    			$this->showMessage('新增失败');
    		}
    	}
    	
        
    }

    public function edit(){
        $id = $this->R('id');
        $info = SdkServerService::get($id);
        
        if(empty($info)) $this->showMessage('找不到该信息');
        
        $this->setTitle('编辑SDK服务器', $info['server_name']);

        $this->out['info'] = $info;

        $backUrl = PubFunc::buildBackUrl("/oss/?r=sdkServer/index");  //返回路径
        $this->out['backUrl'] = $backUrl;
        
        $page = $this->R('page');
        $this->out['page'] = $page;
    }

    public function doEdit(){
        $id = $this->R('id');
        $count = SdkServerService::crepeat($this->R('id'), 'server_name', $this->R('server_name'),$this->R('g'));
        if($count > 0){
        	$this->showMessage('名称已经存在');
        }else{
        	$result = SdkServerService::doEdit($id, $_POST);
        	
            if($result['state'] !== false){
        		$this->showMessage('编辑成功', PubFunc::buildBackUrl("/oss/?r=sdkServer/index"));
        	}else{
        		$this->showMessage('编辑失败');
        	}
        }
        
    }
    
    public function delete(){
    	$id = $this->R('id');
    	$result = SdkServerService::delete($id);
    	 
    	if($result !== false){
    		$this->showMessage('删除成功', PubFunc::buildBackUrl("/oss/?r=sdkServer/index"));
    	}else{
    		$this->showMessage('删除失败', PubFunc::buildBackUrl("/oss/?r=sdkServer/index"));
    	}
    }
    

    
}